<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增实体
            </el-header>
            <el-main>
                <el-form ref="addAttrData" :rules="rules" :model="addAttrData" label-width="115px"
                         label-position="left">
                    <el-form-item label="实体名称 :" prop="name">
                        <el-input v-model="addAttrData.name" placeholder="请输入属性名称"></el-input>
                    </el-form-item>
                    <el-form-item label="实体类型 :" prop="enType">
                        <el-select v-model="addAttrData.enType" filterable placeholder="请选择类型" style="width: 100%;" @change="changeValue">
                            <el-option
                                    v-for="( ky, val ) in enTypeOptions"
                                    :key="val"
                                    :label="ky"
                                    :value="val">
                                {{ky}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="关联设施名称：" prop="refName">
                        <el-input v-show="addAttrData.enType === 'Virtual'" v-model="addAttrData.refName" placeholder="请输入虚拟名称 "></el-input>
                        <el-select v-show="addAttrData.enType === 'Dev'" v-model="addAttrData.refName" filterable placeholder="请选择设备" style="width: 100%;" @change="changeSmt">
                            <el-option
                                    v-for="item in smtOptions"
                                    :key="item.uuid"
                                    :label="item.name"
                                    :value="item.name">
                                {{item.name}}
                            </el-option>
                        </el-select>
                        <comtree v-show="addAttrData.enType === 'Yard'" @treeSelected = "treeSelected" v-model="addAttrData.refName" ref="comtree"></comtree>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';
    import comtree from '../../public/comtree.vue'

    export default {
        components: {comtree},
        name: "addAttr",
        data() {
            return {
                uuid: '',
                addAttrData: {
                    name: '',
                    enType:'',
                    refUuid:'',
                    refName:''
                },
                enTypeOptions: {},
                smtOptions:[],
                rules: {
                    name: [
                        {required: true, message: '事件实体名称不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
            this.queryEnType();
            this.querySmt();
            this.$refs.comtree.autoWidth = '200'
            this.$refs.comtree.autoHeight = '150'
        },
        methods: {
            changeSmt(){
                this.smtOptions.forEach( list =>{
                    if(list.name === this.addAttrData.refName){
                        this.addAttrData.refName = list.name
                        this.addAttrData.refUuid = list.uuid
                    }
                })
            },
            // 获取当前树节点所有信息
            treeSelected (treeNode) {
                this.addAttrData.refName = treeNode.name
                this.addAttrData.refUuid = treeNode.id
            },
            //获取设备所有数据
            querySmt() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/dev/search/findAll",
                    params: {}
                }).then(response => {
                    this.smtOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            changeValue(){
                this.addAttrData.refName = '';
                this.$refs.comtree.inputValue = '';

            },
            //查询后端事件类型标识枚举
            queryEnType() {
                this.axios
                    .request({
                        method: "get",
                        url: "./smt/kdi/enums/EnType",
                        params: {}
                    })
                    .then(response => {
                        this.enTypeOptions = response.data;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            submit() {
                this.$refs['addAttrData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.addAttrData = replaceSpacesObj(this.addAttrData);
                    this.axios.request({
                        method: "post",
                        url: "./smt/kdi/enty",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.addAttrData.name,
                            enType: this.addAttrData.enType,
                            refUuid: this.addAttrData.refUuid,
                            refName: this.addAttrData.refName
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '事件实体新增失败';
                        this.infoShow = true;
                    });
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanType();
                    this.info = '';
                }, 2000);
            },
            cleanType() {
                this.addAttrData = {
                    name: '',
                    enType:'',
                    refUuid:'',
                    refName:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanType();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 100%;
        z-index: 100;
    }


</style>